﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CPAScoringPerformanceTester.Models;
using System.Diagnostics;

namespace CPAScoringPerformanceTester
{
    public static class ServiceProxy
    {
        public static ServiceClient NewProxy()
        {
            try
            {
                return new ServiceClient(new BasicHttpBinding(), new EndpointAddress(new Uri(Options.Current.ServiceUrl)));;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }

        public static void CloseConnection(this ICommunicationObject myServiceClient)
        {
            if (myServiceClient == null) return;

            if (myServiceClient.State != CommunicationState.Opened)
            {
                return;
            }

            try
            {
                myServiceClient.Close();
            }
            catch (CommunicationException ex)
            {
                Debug.Print(ex.ToString());
                myServiceClient.Abort();
            }
            catch (TimeoutException ex)
            {
                Debug.Print(ex.ToString());
                myServiceClient.Abort();
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
                myServiceClient.Abort();
                throw;
            }
            finally
            {
                myServiceClient = null;
            }
        }
    }
}
